//
// Created by xili on 2024/9/18 18:37.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int totalFruit(vector<int> &fruits) {
        int ans = 0;
        int l = 0, r = 0;
        int n = fruits.size();
        unordered_map<int, int> ump;
        for (; r < n; r++) {
            ump[fruits[r]]++;
            while (ump.size() > 2) {
                ump[fruits[l]]--;
                if (!ump[fruits[l]]) {
                    ump.erase(fruits[l]);
                }
                ++l;
            }
            ans = max(ans, r - l + 1);
        }
        return ans;
    }
};